﻿using System;

namespace PropertyExpression.ControllableQuery
{
    public class Lazy<T>
    {
        private readonly Func<T> func;

        public Lazy(Func<T> func)
        {
            this.func = func;
        }

        private T value;
        public T Value
        {
            get
            {
                if (valueIsSet == false)
                {
                    SetValue(func());
                }
                return value;
            }
        }

        private bool valueIsSet;
        private void SetValue(T aValue)
        {
            value = aValue;
            valueIsSet = true;
        }
    }

    public static class Lazy
    {
        public static Lazy<T> Create<T>(Func<T> func)
        {
            return new Lazy<T>(func);
        }
    }
}
